package com.asdhawaii.otello.util;

import java.awt.Toolkit;
import java.net.URL;
import javax.swing.ImageIcon;

/**
 * Accesses and provides images from the program resources folder.
 * 
 * @author Branden Ogata
 *
 */


public class ImageLoader
{
  /**
   * A hidden constructor to prevent instantiation of this class.
   * 
   */
  
  protected ImageLoader()
  {
    System.err.println("Program execution should never reach this.");
  }
  
  /**
   * Returns an image as an ImageIcon.
   * 
   * @param source The Object from which this method was called.
   * @param name The String containing the filename of the image to return.
   * 
   * @return An ImageIcon containing an image.
   *         If no such image exists in the resources folder, returns null.
   * 
   */
  
  public static ImageIcon getImageIcon(Object source, String name)
  {
//    System.out.println("Started getImageIcon");
    Toolkit toolkit = Toolkit.getDefaultToolkit();
//    System.out.println("Opened toolkit");
    URL imageUrl = source.getClass().getResource("/resources/" + name);
//    System.out.println("/resources/" + name + " -> " + imageUrl);
//    return (imageUrl == null) ? (null) : (new ImageIcon(toolkit.createImage(imageUrl)));
    if (imageUrl == null)
    {
      System.err.println("Could not find " + name);
      return null;
    }
    else
    {
      ImageIcon icon = new ImageIcon(toolkit.createImage(imageUrl));
//      System.out.println("\"" + icon + "\"");
      return icon;
    }
  }  
}
